<?php
/**
 * Geneone: Content Management Framework
 * Forum Centre Object View
 *
 * http://geneone.neondragon.net/
 *
 * @package Geneone
 * @author Khlo
 * @version 1.0
 * @copyright 2004-2006 Geneone Development Team
 */
 
require_once ("Gene/Object/View.php");

class Gene_Object_View_Forumcentre extends Gene_Object_View {
	/**
	 * Class Constructor
	 *
	 * @var object $model Gene_Object_Model object
	 */
	function Gene_Object_View_Forumcentre(&$model) {
		$this->model =& $model;
	}
	
	/**
	 * Show this object. Returns HTML output code for object.
	 *
	 * @var array $actions Additional Arguments
	 *
	 * @return string HTML output for object.
	 */
	function view($actions=array()) {
		$tpl =& Gene::getTemplate();
		$db =& Gene::getDatabase();
		$user =& Gene::getUser();
		
		/* General Object Information */
		$info = $this->model->get();
		
		$tpl->assign ("pagetitle", $info['title']);
		$tpl->assign ("object", $info);
		
		/* Parents */
		$parents = $this->model->getParents();
		$tpl->assign ("parents", $parents);
		
		/* Views */
		if ($this->model->getUserProperty("view")) {
			$view = $this->model->getUserProperty("view");
		} else if ($this->model->getProperty("defaultview")) {
			$view = $this->model->getProperty("defaultview");
		} else {
			$view = "list";
		}
		
		$tpl->assign ("defaultview", $view);
		
		if (isset($actions[2])) {
			if ($actions[2] == "preferences" && $user->getUserId()) {
				if ($_SERVER['REQUEST_METHOD'] == 'POST') {
					$view = Gene_Request::getString("view", GENE_REQUEST_POST);
					if ($view && ($view == "smart" OR $view == "list")) {
						$this->model->setUserProperty("view", $view);
					}
					header ("Location: ".Gene::makeLink($info));
					exit();
				} else {
					return $tpl->fetch("objects/forumcentre/view_preferences");
				}
			}
			if ($actions[2] == "smart" && $user->getUserId()) {
				$view = "smart";
			} else {
				$view = "list";
			}
		}
		
		$tpl->assign ("view", $view);
		
		/* Childs */
		$read = $db->extended->getAssoc("SELECT forum_id, time FROM ".Gene::tableName("forum_read")." WHERE user_id=? AND parent_id=?", null, array($user->getUserId(), $info['object_id']));
		
		if ($view == "smart") {
			if ($this->model->getUserProperty("smart_update") < time()-1814400) { /* Regenerate favourite forums every 3 weeks */
				$this->model->updateMyForums();
			}
			$childs = $this->model->getMyForums();
		} else {
			$childs = $this->model->getChilds(array(
				"tables" => array(Gene::tableName("discussions"), Gene::tableName("forums")),
				"order" => "priority DESC, numchilds DESC",
			));
		}
		
		if (count($childs)) {
			/* Get last post object information */
			foreach ($childs as $i) {
				if ($i['last_comment_id']) {
					$lastids[] = $i['last_comment_id'];
				}
			}
			
			$lastinfo = $db->extended->getAssoc("SELECT g.*, o.*, u.name FROM ".Gene::tableName("objects")." o LEFT JOIN ".Gene::tableName("user_info")." u ON (o.owner_id=u.user_id) LEFT JOIN ".Gene::tableName("object_guests")." g ON (o.object_id=g.object_id) WHERE o.object_id IN (".implode(",", array_unique($lastids)).")");
			
			foreach ($lastinfo as $j => $k) {
				$lastinfo[$j]['object_id'] = $j;
			}
			
			foreach ($childs as $j => $k) {
				if (isset($lastinfo[$k['last_comment_id']])) {
					if (!isset($read[$k['object_id']]) OR $lastinfo[$k['last_comment_id']]['t_create'] > $read[$k['object_id']]) {
						$childs[$j]['unread'] = true;
					} else {
						$childs[$j]['unread'] = false;
					}
					
					$childs[$j]['lastcomment'] = $lastinfo[$k['last_comment_id']];
				} else {
					$childs[$j]['unread'] = false;
				}
			}
		}
		
		$tpl->assign ("childs", $childs);
		
		$tpl->append ("portlets", "forumcentre");
		
		if ($view == "smart" && $user->getUserId()) {
			// Get Recently Viewed Discussions
			$db->setLimit(5);
			
			$q = $db->prepare ("SELECT g.*, r.*, o.*, fi.*, u.name FROM ".Gene::tableName("discussion_read")." r LEFT JOIN ".Gene::tableName("objects")." f ON (r.forum_id=f.object_id) LEFT JOIN ".Gene::tableName("forums")." fi ON (r.forum_id=fi.object_id) LEFT JOIN ".Gene::tableName("objects")." o ON (r.discussion_id=o.object_id) LEFT JOIN ".Gene::tableName("user_info")." u ON (o.owner_id=u.user_id) LEFT JOIN ".Gene::tableName("object_guests")." g ON (o.object_id=g.object_id) WHERE r.user_id=? AND f.parentcsv LIKE ? AND o.type='discussion' ORDER BY r.time DESC");
			$res = $q->execute(array($user->getUserId(), $info['parentcsv'].",".$info['object_id']."%"));

			$recent = array();
			while ($row = $res->fetchRow()) {
				$row['excerpt'] = strip_tags($row['content']);
				if (strlen($row['excerpt']) > 100) {
					$row['excerpt'] = substr($row['excerpt'], 0, 100);
					$s = explode(" ", $row['excerpt']);
					$c = count($s) - 1;
					unset($s[$c]);
					$row['excerpt'] = implode(" ", $s)."...";
				}
					
				$recent[] = $row;
			}
			
			$tpl->assign ("recent", $recent);
			
			return $tpl->fetch("objects/forumcentre/view_smart");
		} else {
			return $tpl->fetch("objects/forumcentre/view");
		}
	}
	
	/**
	 * Displays the object creation form for this type of object.
	 *
	 * @static
	 *
	 * @var object $parent Parent Object
	 *
	 * @return string HTML output for object creation.
	 */
	function create(&$parent) {
		$tpl =& Gene::getTemplate();
		return $tpl->fetch("objects/forumcentre/create");
	}
}
 
?>